package com.example.springboot2.DB_Helper;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;


public class DataBase<T> {
    //增删改的封装
    public String add(T t) throws IllegalAccessException {
        /*
         * 通过userBean自动生成insert语句
         *insert into product(price,name,id) values('5000.0','iphone','1')
         */
        Class clazz = t.getClass();//返回此 t的运行时类。
        /*通过运行实类，可以调用反射中的
        getDeclaredAnnotation(返回对象全部注解),
        getDeclaredFields（返回对象的全部属性）
        getDeclaredMethods（返回对象的全部方法）*/
        Table table = (Table) clazz.getDeclaredAnnotation(Table.class);

        PrimeKey primeKey=(PrimeKey) clazz.getDeclaredAnnotation(PrimeKey.class);

        String tableName = table.value();
        String promeKeyName=primeKey.value();

        System.out.println(tableName);
        Field[] fields = clazz.getDeclaredFields();
        String keys = "";
        String values = "";
        for(int i=0;i<fields.length;i++)
        {
            IsMap Map=(IsMap) fields[i].getDeclaredAnnotation(IsMap.class);
            if(Map!=null)
            {
                String MapValue=Map.value();
                if(MapValue.equals("否"))
                {
                    continue;
                }
            }
            if(fields[i].get(t)==null)
            {
                continue;
            }
            if(keys.length()>0) {
                keys += ",";
            }
            if(values.length()>0) {
                values += ",";
            }

               keys += fields[i].getName();
               values += "'"+fields[i].get(t)+"'";
        }
        String sql = "insert into "+tableName+"("+keys+") values("+values+")";
        System.out.println(sql);
        return sql;
    }
    public String update(T t)throws IllegalAccessException{
        Class clazz = t.getClass();
        Table table = (Table) clazz.getDeclaredAnnotation(Table.class);

        PrimeKey primeKey=(PrimeKey) clazz.getDeclaredAnnotation(PrimeKey.class);

        String tableName = table.value();
        String primeKeyName=primeKey.value();
        String primeKeyValue="";
        System.out.println(tableName);
        Field[] fields = clazz.getDeclaredFields();
        String keyvaluess = "";
        for(int i=0;i<fields.length;i++)
        {
            if(fields[i].getName().equals(primeKeyName))
            {
                primeKeyValue="'"+(String)fields[i].get(t)+"'";
                continue;
            }
            IsMap Map=(IsMap) fields[i].getDeclaredAnnotation(IsMap.class);
            if(Map!=null)
            {
                String MapValue=Map.value();
                if(MapValue.equals("否"))
                {
                    continue;
                }
            }
            if(fields[i].get(t)==null)
            {
                continue;
            }
            if(keyvaluess.length()>0) {
                keyvaluess += ",";
            }
            keyvaluess+=fields[i].getName()+"='"+fields[i].get(t)+"'";
        }
        String sql = " update "+tableName+" set "+keyvaluess +" where "+primeKeyName+"="+primeKeyValue;
        System.out.println(sql);
        return sql;
    }
}
